package com.college.vod.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.college.commonutil.exceptionhandler.GuliException;
import com.college.commonutil.result.Result;
import com.college.vod.client.ServiceEduClient;
import com.college.vod.entity.EduCourse;
import com.college.vod.service.VodService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/vod")
@CrossOrigin
public class VodController {
    @Autowired
    private VodService vodService;
    @Autowired
    ServiceEduClient eduClient;
    //上传
    @PostMapping("upload")
    public Result uploadFile(MultipartFile file){
        String videoId=vodService.uploadVideo(file);
        return Result.ok().data("videoId",videoId);
    }
    //根据视频id从阿里云删除视频
    @DeleteMapping("removeAlyVideo/{id}")
    public Result removeAlyVideo(@PathVariable String id){
        vodService.removeAlyVideo(id);
        return Result.ok();
    }
    //根据视频id获取凭证
    @GetMapping("/getPlayAuth/{id}")
    public Result getPlayAuth(@PathVariable String id){
        try {
            String playAuth = vodService.getPlayAuth(id);
            //获取课程id修改播放量
            String courseId=eduClient.getVideoInfoBySourse(id).getCourseId();
            eduClient.viewcount(courseId);

            //获取播放历史集合
            List<EduCourse> list=eduClient.getCourseInfoHistory(courseId);
            System.out.println(playAuth);
            list.forEach(System.out::println);
            return Result.ok().data("PlayAuth",playAuth).data("list",list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001,"获取视频凭证失败");
        }
    }
}
